<?php
namespace User\Controller;
use Spark\Util\Page;
class MemberController extends UserController{
	public function index(){
		
		$where = $this->_search();
		$model = M('juejin_users');
		$count = $model->where($where)->count();
		$Page       = new Page($count,20);
		$show       = $Page->show();
		$list = $model->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
		
		$userlist = $model->select();
		foreach ($list as $key =>$val){	
			$list[$key]['num']=$this->getChildCount($userlist,$val['id'],0);
		}
		$this->assign('list',$list);
		$this->assign('page',$show);
     
		$this->display();
	}
	public function mychild(){
		$pid = I("pid");
		$lever = I("lever");
		$model = M('juejin_users');
		$userlist = $model->select();
		
		$userinfo = $model->find($pid);
		switch ($lever){
			case 0:
				$userinfo['title']='一级人数';
				break;
			case 1:
				$userinfo['title']='二级人数';
				break;
			case 2:
				$userinfo['title']='三级人数';
				break;
			
		}
		
		
		$list=$this->getChildVal($userlist,$pid,0,$lever);
		
		
		foreach ($list as $key =>$val){
			$list[$key]['num']=$this->getChildCount($userlist,$val['id'],0);
		}
		
		$this->assign('userinfo',$userinfo);
		$this->assign('list',$list);
		$this->display();
		
	}
	function getChildCount($arr,$pid,$num){
		
		$data = array();
		if($num<3){
			foreach($arr as $key=> $v){
				if($v['shareid'] == $pid){	
					$data[$num]['count'] +=1;	
					$data = array_merge($data,self::getChildCount($arr,$v['id'],$num+1));
				}
			}
		}
		return $data;
		
	}
	
	function getChildVal($arr,$pid,$num,$lever=0){
		$data = array();
		if($num<3){
			foreach($arr as $key=> $v){
				if($v['shareid'] == $pid){
					if($num ==$lever){
						$data[$key] = $v;
						$data[$key]['daishu'] = $num;
							
					}
					
					$data = array_merge($data,self::getChildVal($arr,$v['id'],$num+1,$lever));
				}
			
			}
		}
		
		return $data;
	}
	
	
	
	public function tongji(){
		
		$count = M('member_recharge_record')->where(['token'=>session('token')])->count();
		$Page       = new Page($count,20);
		$show       = $Page->show();
		$list = M('member_recharge_record')->where(['token'=>session('token')])->limit($Page->firstRow.','.$Page->listRows)->order('time desc')->select();
		$model = M('Member_card_view');
		foreach ($list as $key=>$val){
			$data = array();
			$data['uid']=session('uid');
			$data['wechat_id']=$val['wechat_id'];	
			$user = $model->where($data)->find();
			$list[$key]['truename']=$user['truename'];
			$list[$key]['type_name']=$user['type_name'];
			$list[$key]['tel']=$user['tel'];
			$list[$key]['number']=$user['number'];		
		}
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display();
	
	}
	private function _search(){
		(I('wechat_name') && ($search['wechat_name'] = ['like','%'.I('wechat_name').'%']));
		(I('number') && ($search['number'] = ['like','%'.I('number').'%']));
		(I('phone') && ($search['phone'] = ['like','%'.I('phone').'%']));
		return $search;
	}
	
	public function yongjin(){
		
		$search = $this->_search();
		$where = array();
		if($search){
			$user = M('juejin_users')->where($search)->find();
			$where['userid']=$user['id'];
		}
		$model = M('juejin_tiorder');
		$count = $model->where($where)->count();
		$Page       = new Page($count,20);
		$show       = $Page->show();
		$list = $model->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
		foreach ($list as $key=>$val){
			$user = M('juejin_users')->find($val['userid']);
			$list[$key]['wechat_name']=$user['wechat_name'];
			$list[$key]['phone']=$user['phone'];
			
		}
		
		$this->assign('list',$list);
		$this->assign('page',$show);
		 
		$this->display();

	}
	public function order(){

		$search = $this->_search();
		$where = ['is_sim'=>1];
		if($search){
			$user = M('juejin_users')->where($search)->find();
			$where['userid']=$user['id'];
		}	
		
		$model = M('juejin_order');
		
		$count = $model->where($where)->count();
		$Page       = new Page($count,20);
		$show       = $Page->show();
		$list = $model->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
		foreach ($list as $key=>$val){
			$user = M('juejin_users')->find($val['userid']);
			$list[$key]['wechat_name']=$user['wechat_name'];
			$list[$key]['phone']=$user['phone'];
			
		}	
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display();
	}
	public function xuorder(){


		$search = $this->_search();
		$where = ['is_sim'=>0];
		if($search){
			$user = M('juejin_users')->where($search)->find();
			$where['userid']=$user['id'];
		}	
		$model = M('juejin_order');
		
		$count = $model->where($where)->count();
		$Page       = new Page($count,20);
		$show       = $Page->show();
		$list = $model->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
		foreach ($list as $key=>$val){
			$user = M('juejin_users')->find($val['userid']);
			$list[$key]['wechat_name']=$user['wechat_name'];
			$list[$key]['phone']=$user['phone'];		
		}
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display();
	}
	
	
	public function phb(){
		$sql = "SELECT userid,SUM(amount) as amount FROM `sp_juejin_order` where is_sim=1 GROUP BY userid ORDER BY  SUM(amount) DESC";

		$list = M('juejin_order')->query($sql);
		foreach ($list as $key=>$val){
			$user = M('juejin_users')->find($val['userid']);
			//总充值金额
			$list[$key]['paysum']=M("juejin_payorder")->where(['userid'=>$val['userid'],'status'=>2])->sum('price');
			//总提现金额
			$list[$key]['sucsum']=M("juejin_tiorder")->where(['userid'=>$val['userid'],'status'=>2])->sum('price');
			//待确定提现
			$list[$key]['errsum']=M("juejin_tiorder")->where(['userid'=>$val['userid'],'status'=>0])->sum('price');
			
			if(!$user){unset($list[$key]);continue;}
			$list[$key]['wechat_name']=$user['wechat_name'];
			$list[$key]['phone']=$user['phone'];
			
		}	
		$this->assign('list',$list);
		$this->display();


	}
	public function pay(){
		$search = $this->_search();
		$where = array();
		if($search){
			$user = M('juejin_users')->where($search)->find();
			$where['userid']=$user['id'];
		}

		$model = M('juejin_payorder');
		$count = $model->where($where)->count();
		$Page       = new Page($count,20);
		$show       = $Page->show();
		$list = $model->where($where)->limit($Page->firstRow.','.$Page->listRows)->order('id desc')->select();
		foreach ($list as $key=>$val){
			$user = M('juejin_users')->find($val['userid']);
			$list[$key]['wechat_name']=$user['wechat_name'];
			$list[$key]['phone']=$user['phone'];
			
		}
		
		$this->assign('list',$list);
		$this->assign('page',$show);

		
		$this->display();
	}
	public function queren_yongjin(){
		$orderid = I("id");
		$info  = M('juejin_tiorder')->find($orderid);
		if($info){
			$res = M('juejin_tiorder')->where(['id'=>$orderid])->setField('status',2);
			if($res){
				$this->success('提现确认成功',U('Member/yongjin'));
			}else{
				$this->error('确认失败，请重试');
			}
		}else{
			$this->error('无效订单号，请确定');
		}
		
		
	}
	public function back_yongjin(){
		$orderid = I("id");
		$info  = M('juejin_tiorder')->find($orderid);
		if($info){
			$res = M('juejin_tiorder')->where(['id'=>$orderid])->setField('status',1);
			
			M('juejin_users')->where(['id'=>$info['userid']])->setInc('addmoney',$info['addmoney']);
			if($info['is_over']==1){
				$money = $info['price'];
			}else{
				$money = $info['price']*2;
			}
			M('juejin_users')->where(['id'=>$info['userid']])->setInc('money',$money);
			M('juejin_users')->where(['id'=>$info['userid']])->setDec('dongjie',$info['price']);
			if($res){
				$this->success('提现退回成功',U('Member/yongjin'));
			}else{
				$this->error('操作失败，请重试');
			}
		}else{
			$this->error('无效订单号，请确定');
		}
		
		
	}
	
	public function add(){
		$sql=M('Member');
		$data['token']=session('token');
		$data['uid']=session('uid');
		$member=$sql->field('id')->where($data)->find();
		$pic['homepic']=I('post.homepic');
		if($member!=false){
			$back=$sql->where($data)->save($pic);
			if($back){
				$this->success('更新成功');
			}
			else{
				$this->error('服务器繁忙，请稍后再试1');
			}
		}else{
			$data['homepic']=$pic['homepic'];
			$back=$sql->add($data);
			if($back){
				$this->success('更新成功');
			}
			else{
				$this->error('服务器繁忙，请稍后再试');
			}
		}
	}
	
	//删除会员
	public function del(){
	
		$data['id'] = I('get.id');
		$member = M('juejin_users')->where($data)->find();
		if($member == null) $this->error('会员不存在！');
		$status = I("status");
		$res = M('juejin_users')->where($data)->save(['status'=>$status,'updatetime'=>time()]);
		if($res){
			$this->success('操作成功！');
		}
		else{
			$this->error('操作失败！');
		}
	}
	
	//会员详细信息显示与编辑
	public function detail(){
		if(IS_POST){
			$model = M('juejin_users');
			$model->create();
			$ret = $model->save();
			if($ret){
				$this->success('保存成功！');
			}
			else{
				$this->error('保存失败！');
			}
		}
		else{
			$mid = I('get.id',0,'intval');
			
			$model = M('juejin_users');
		
		
			$where['id'] = $mid;
			
			
			$member = $model->where($where)->find();

			$this->assign('member',$member);
			
			$this->display();
		}
	}
	
	//更改会员卡类型
	public function change_card_level(){
		$card_type = I('post.card_type',0,'intval');
		$wechat_id = I('post.wechat_id','','trim');
		(!$wechat_id || !$card_type) && $this->error('目标级别会员卡不存在！');
		$cardType = M('member_card_set')->field('type_name')->where(['token'=>session('token'),'id'=>$card_type])->find();
		//查找新卡
		$card = M('member_card_create')->where(['card_id'=>$card_type,'token'=>session('token')])->order('id asc')->find();
		!$card && $this->error($cardType['type_name'].'已被领完！');
		//解绑旧卡
		M('member_card_create')->where(['wechat_id'=>$wechat_id,'token'=>session('token')])->setField('wechat_id','');
		//设置新卡
		$ret = M('member_card_create')->where(['number'=>$card['number'],'token'=>session('token')])->setField('wechat_id',$wechat_id);
		if($ret)$this->success('设置成功！');
		else $this->error('设置失败！');
	}
	
	//会员消费管理
	public function expense(){
		$mid = I('get.id',0,'intval');
		$member = M('member_info')->field('wechat_id')->find($mid);
		$wechat_id = $member['wechat_id'];
		if(IS_POST){
			//在会员信息里增加消费金额和积分
			$logic = new \Wap\Logic\Member(session('token'),$wechat_id);
			$data['money'] = $_POST['expense'];
			$data['time'] = strtotime($_POST['time']);
			$data['remark'] = $_POST['remark'];
			
			$ret = $logic->expense($data,$_POST['score']);
			if($ret){
				$this->success('添加成功！');
			}
			else{
				$this->error('添加失败！');
			}
		}
		else{
			$list = M('member_expense')->where(['token'=>session('token'),'wechat_id'=>$member['wechat_id']])->order('id desc')->select();
			$this->assign('list',$list);
			$this->display();
		}
	}
	
	//删除消费记录
	public function expense_del(){
		$id = I('get.id',0,'intval');
		$uid = I('get.uid',0,'intval');
		$info = M('member_expense')->find($id);
		if($info ==null){
			$this->error('非法操作!',U('Member/expense?id='.$uid));exit;
		}
		$ret = M('member_expense')->where(['token'=>session('token'),'id'=>$id])->delete();
		if($ret){
			$where = ['token'=>session('token'),'wechat_id'=>$info['wechat_id']];
			M('member_info')->where($where)->setDec('total_expense',$info['expense']);
			M('member_info')->where($where)->setDec('expense_score',$info['score']);
			M('member_info')->where($where)->setDec('total_score',$info['score']);
			$this->success('删除成功!',U('Member/expense?id='.$uid));
		}
		else{
			$this->error('非法操作!',U('Member/expense?id='.$uid));
		}
	}
	
	//会员积分管理
	public function score(){
		$mid = I('get.id',0,'intval');
		$member = M('member_info')->field('wechat_id')->find($mid);
		$wechat_id = $member['wechat_id'];
		if(IS_POST){
			$score = I('post.score',0,'intval');
			$remark = I('post.remark','','trim');
			$logic = new \Wap\Logic\Member(session('token'),$member['wechat_id']);
			$ret = $logic->alterScore($score,4,$remark);
			if($ret){
				$this->success('添加成功！');
			}
			else{
				$this->error('添加失败！');
			}
		}
		else{
			$list = M('member_score_record')->where(['wechat_id'=>$member['wechat_id']])->order('id desc')->select();
			$this->assign('list',$list);
			$this->display();
		}
	}
	
	public function score_del(){
		$id = I('get.id',0,'intval');
		$uid = I('get.uid',0,'intval');
		$info = M('member_score_record')->find($id);
		if($info ==null){
			$this->error('非法操作!',U('Member/score?id='.$uid));exit;
		}
		$ret = M('member_score_record')->where(['token'=>session('token'),'id'=>$id])->delete();
		if($ret){
			$where = ['token'=>session('token'),'wechat_id'=>$info['wechat_id']];
			M('member_info')->where($where)->setDec('total_score',$info['score']);
			$this->success('删除成功!',U('Member/score?id='.$uid));
		}
		else{
			$this->error('非法操作!',U('Member/score?id='.$uid));
		}
	}
	
	//会员申请审核
	public function check(){
		$list = M('member_info')->where(['token'=>session('token'),'status'=>0])->select();
		$this->assign('list',$list);
		$this->display();
	}
	
	public function check_request(){
		$id = I('get.id',0,'intval');
		$member = M('member_info')->field('wechat_id')->where(['token'=>session('token'),'id'=>$id])->find();
		$cardset = M('Member_card_set')->where(['token'=>session('token'),'base_points'=>0])->field('id')->find();
		$card = M('Member_card_create')->field('id,number')->where("token = '".session('token')."' and wechat_id = '' and card_id=".$cardset['id'])->find();
		$exchange = M('Member_card_exchange')->field('startup_score')->where(array('token'=>session('token')))->find();
		
		//如果商家还有会员卡，可以领
		if($card != null){
			//微信与会员卡绑定
			$card_up=M('Member_card_create')->where(array('id'=>$card['id']))->setField('wechat_id',$member['wechat_id']);
			//记录会员信息
			M('Member_info')->where(['token'=>session('token'),'wechat_id'=>$member['wechat_id']])->setField('status',1);
			if($exchange['startup_score']!=0){
				$logic = new \Wap\Logic\Member(session('token'),$member['wechat_id']);
				$logic->alterScore($exchange['startup_score'],4,'领卡送积分');
			}
			$this->success('审核成功！');
		}
		else{
			//商家没有了会员卡
			$this->error('会员卡已被领完啦！');
		}
	}
	
	public function money(){
		$mid = I('get.id',0,'intval');
		$member = M('juejin_users')->find($mid);
		if(IS_POST){
			$money = floatval(I('post.money'));
			$remark = I('post.remark','','trim');
			
			$data['userid'] = $mid;
			$data['wechat_id'] = $member['wechat_id'];
			$data['money'] = $money;
			$data['time'] = time();
			$data['remark'] = I('post.remark','线下充值','trim');
			$data['status'] = 1;
			$data['type'] = 1;	

			$ret =M('member_recharge_record')->add($data);
			if($ret){
				M('juejin_users')->where(['id'=>$mid])->setInc('money',$money);
				$this->success('充值成功！');
			}
			else{
				$this->error('充值失败！');
			}
		}
		else{
			$list = M('juejin_payorder')->where(['userid'=>$mid])->order('id desc')->select();
			$this->assign('list',$list);
			$this->display();
		}
	}
}
?>